package com.mobileservice.autoreply.ui.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobileservice.autoreply.R;
import com.mobileservice.autoreply.activity.AddContactActivity;
import com.mobileservice.autoreply.activity.BlockMessageActivity;
import com.mobileservice.autoreply.activity.BlockinComingCallActivity;
import com.mobileservice.autoreply.common.constant.CommConstant;
import com.mobileservice.autoreply.ui.model.PhoneContactModel;

public class ContactItemAdapter extends ArrayAdapter<PhoneContactModel> {

	private List<PhoneContactModel> contactModels = new ArrayList<PhoneContactModel>();
	public List<PhoneContactModel> SelectedModel = new ArrayList<PhoneContactModel>();
	private LayoutInflater inflater;
	private int layout;

	/** Contructor */
	public ContactItemAdapter(Context context, int layout, List<PhoneContactModel> contactModels) {
		super(context, layout, contactModels);
		// TODO Auto-generated constructor stub
		this.layout = layout;

		if (contactModels != null) {
			this.contactModels = contactModels;
		}

		this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return contactModels.size();
	}

	@Override
	public PhoneContactModel getItem(int arg0) {
		return contactModels.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {

		final PhoneContactModel model = contactModels.get(arg0);
		Holder holder = null;
		if (arg1 == null) {
			arg1 = inflater.inflate(layout, null);

			holder = new Holder();

			/** Find View by id for Items */
			holder.image = (ImageView) arg1.findViewById(R.id.imageContact);
			holder.ContactName = (TextView) arg1.findViewById(R.id.contactName);
			holder.ContactNumber = (TextView) arg1.findViewById(R.id.contactNumber);
			holder.SelectNum = (CheckBox) arg1.findViewById(R.id.selectionContact);
			holder.auto_reply = (ImageView) arg1.findViewById(R.id.auto_reply);
			/** End */
			arg1.setTag(holder);
		} else {
			holder = (Holder) arg1.getTag();
		}

		holder.ContactName.setText(model.getContactName());
		holder.ContactNumber.setText(model.getContactNumber());

		holder.auto_reply.setVisibility(model.isAutoReply() ? View.VISIBLE : View.INVISIBLE);

		/** Need to work again */
		if (getContext() instanceof AddContactActivity) {
			AddContactActivity adActivity = (AddContactActivity) getContext();

			if (adActivity.CallFromWhich == CommConstant.BLOCK_CALL) {
				SelectColorSelected(holder, !model.isLockCallPhone());
				holder.SelectNum.setChecked(model.isLockCallPhone());
			} else {
				SelectColorSelected(holder, !model.isLockSMS());
				holder.SelectNum.setChecked(model.isLockSMS());
			}

		} else if (getContext() instanceof BlockinComingCallActivity) {
			SelectColorSelected(holder, !model.isLockCallPhone());
			holder.SelectNum.setChecked(model.isLockCallPhone());

		} else if (getContext() instanceof BlockMessageActivity) {
			SelectColorSelected(holder, !model.isLockSMS());
			holder.SelectNum.setChecked(model.isLockSMS());
		}

		/** End */

		final Holder template = (Holder) arg1.getTag();
		/** Set Listener */
		holder.SelectNum.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				CheckBox cb = (CheckBox) v;

				if (getContext() instanceof AddContactActivity) {
					AddContactActivity adActivity = (AddContactActivity) getContext();

					if (adActivity.CallFromWhich == CommConstant.BLOCK_CALL) {
						contactModels.get(arg0).setLockCallPhone(cb.isChecked());
					} else {
						contactModels.get(arg0).setLockSMS(cb.isChecked());
					}

					if (cb.isChecked()) {
						SelectedModel.add(contactModels.get(arg0));
					} else {
						SelectedModel.remove(contactModels.get(arg0));
					}
				} else {

					if (getContext() instanceof BlockinComingCallActivity) {
						contactModels.get(arg0).setLockCallPhone(cb.isChecked());
					} else if (getContext() instanceof BlockMessageActivity) {
						contactModels.get(arg0).setLockSMS(cb.isChecked());

					}

					if (cb.isChecked()) {
						SelectedModel.remove(contactModels.get(arg0));
					} else {
						SelectedModel.add(contactModels.get(arg0));
					}

					/** Set Topbar Button Become Save Button */

					if (getContext() instanceof BlockinComingCallActivity) {

						((BlockinComingCallActivity) getContext()).topBar.shiftButton((SelectedModel.size() > 0));

					} else if (getContext() instanceof BlockMessageActivity) {
						((BlockMessageActivity) getContext()).topBar.shiftButton(SelectedModel.size() > 0);
					}
				}

				SelectColorSelected(template, !cb.isChecked());
			}
		});

		return arg1;
	}

	private void SelectColorSelected(Holder holder, boolean isChecked) {
		/** Check color for Contact Name, Number */

		holder.ContactName.setTextColor(getContext().getResources().getColor(isChecked ? R.color.caption_color : R.color.caption_color_selected));
		holder.ContactNumber.setTextColor(getContext().getResources().getColor(isChecked ? R.color.caption_color : R.color.caption_color_selected));
	}

	private class Holder {
		ImageView image, auto_reply;
		TextView ContactName, ContactNumber;
		CheckBox SelectNum;
	}

}
